<%@ page contentType="text/html; charset=UTF-8" %>
<%@page import="cn.bopomo.dealer.base.common.CommonConstant"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<html:html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
		<title>操作员管理</title>
		<script language="javascript" src="/js/json2.js"></script>
		<script language="javascript" src="/js/litpen_ajax.js"></script>
		<script language="javascript" type="text/javascript">
			function copyAll(from,to){
				fromList = eval('document.forms[0].' + from);
				toList = eval('document.forms[0].' + to);
				if (toList.options.length > 0 && toList.options[0].value == 'temp'){
					toList.options.length = 0;
				}
				for (i=0;i<fromList.options.length;i++){
					var current = fromList.options[i];
					if (current.value == 'temp'){
							alert ('你不能选择这个项目!');
							return;
					}
					txt = current.text;
					val = current.value;
					toList.options[toList.length] = new Option(txt,val);
					fromList.options[i] = null;
					i--;
				}
			}

			function copyToList(from,to){
				fromList = eval('document.forms[0].' + from);
				toList = eval('document.forms[0].' + to);
				if (toList.options.length > 0 && toList.options[0].value == 'temp'){
					toList.options.length = 0;
				}
				var sel = false;
				for (i=0;i<fromList.options.length;i++){
					var current = fromList.options[i];
					if (current.selected){
						sel = true;
						if (current.value == 'temp'){
							alert ('你不能选择这个项目!');
							return;
						}
						txt = current.text;
						val = current.value;
						toList.options[toList.length] = new Option(txt,val);
						
						fromList.options[i] = null;
						i--;
					}
				}
				if (!sel) alert ('你还没有选择任何项目');
			}
			
			function showDiv(id,cbFunction){
				var moveDiv = document.getElementById("moveDiv");
				
				moveDiv.style.left = document.body.clientWidth / 2 - 360/2;
				
				moveDiv.style.top = document.body.clientHeight / 2 - 200/2;
				var selects = document.getElementsByTagName("select");
				for (var i = 0; i<selects.length;i++){
					selects[i].style.display = "none";
				}
				var form_selects = moveDiv.getElementsByTagName("select");
				for (var j = 0; j<form_selects.length;j++){
					form_selects[j].style.display = "block";
				}
				
				cbFunction = arguments[1];
				id = arguments[0];
				
				var action = "";
				if (id == 'undefined' || id == null || id == 'null'){
					action = "/admin/loadOPGroupAjax.do"; //add
				}else{
					action = "/admin/loadOPGroupAjax.do?oid=" + id; //modify
				}
				
				var form = document.forms['OperatorForm'];
				clearForm(form);
				
				var sendAjax = new Ajax(action,null,true,cbFunction,"get");
				sendAjax.send();
				
				moveDiv.style.display = "block";
			}
			
			function addOPCallBack(){
				
				var result = arguments[0];
				var myData = JSON.parse(result,null);
				var form = document.forms['OperatorForm'];
				var toList = form.to;
				var fromList = form.from;
				toList.innerHTML = "";
				fromList.innerHTML = "";
				for (var i = 0; i<myData.toArray.length;i++){
					var txt = myData.toArray[i].groupName;
					var val = myData.toArray[i].gid;
					toList.options[i] = new Option(txt,val);
				}
				for (var j = 0; j<myData.fromArray.length;j++){
					var txt = myData.fromArray[j].groupName;
					var val = myData.fromArray[j].gid;
					fromList.options[j] = new Option(txt,val);
				}
				form.parentId.value = myData.pid;
				
			}
			
			
			function editOPCallBack(){
				var result = arguments[0];
				var myData = JSON.parse(result,null);
				var form = document.forms['OperatorForm'];
				var toList = form.to;
				var fromList = form.from;
				toList.innerHTML = "";
				fromList.innerHTML = "";
				for (var i = 0; i<myData.toArray.length;i++){
					var txt = myData.toArray[i].groupName;
					var val = myData.toArray[i].gid;
					toList.options[i] = new Option(txt,val);
				}
				for (var j = 0; j<myData.fromArray.length;j++){
					var txt = myData.fromArray[j].groupName;
					var val = myData.fromArray[j].gid;
					fromList.options[j] = new Option(txt,val);
				}
				
				form.parentId.value = myData.pid;
				form.id.value = myData.id;
				form.loginName.value = myData.loginName;
				if (myData.hasChild == "1")
					form.hasChild.checked = true;
				else
					form.hasChild.checked = false;
				form.introduction.value = myData.introduction;
				form.btnAction.innerHTML = "修改";
			}
			
			function clearForm(form){
				//alert(form.length);
				for (var i = 0;i<form.length;i++){
					e=form[i];
					if (e.name!='')
					{
						if (e.type=='select-multiple' || e.type=='select-one')
						{
							
							e.innerHTML = "";
						}
						else if (e.type=='checkbox' || e.type=='radio' )
						{
							if (e.checked==false)
							{
								continue;	
							}
							e.checked = false;
						}
						else if (e.type=='submit' || e.type=='reset' || e.type =='button')
						{
							continue;
						}
						else 
						{
							e.value = "";
						}
						
					}
				}
				form.btnAction.innerHTML = "增加";
				form.action = "/admin/addOperatorAjax.do";
			}
			
			function closeDiv(){
				var selects = document.getElementsByTagName("select");
				for (var i = 0; i<selects.length;i++){
					selects[i].style.display = "";
				}
				var moveDiv = document.getElementById("moveDiv");
				moveDiv.style.display = "none";
			}
			
			function authorizeInit(form){
				var str = "";
				for (var i=0;i<form.to.length;i++){
					str += form.to[i].value + "/";
				}
				form.authorize.value= str;
				
			}
		/* wen qing here*/	
			






			
			var oid = <%=request.getParameter("oid")%>
			
			
			function gotoPage(url){
				//alert(url);
				loadOperatorList(url);
			}
			
			
			
			/* -----发送AJAX请求_获取操作员列表-----*/
			function loadOperatorList1(){
				var loadOperatorList_ajax = new Ajax("/admin/loadOperatorListAjax.do?oid="+oid,null,true,loadOperatorListCallback,"get");
				loadOperatorList_ajax.send();
			}
			
			function loadOperatorList(url){
				var loadOperatorList_ajax = new Ajax(
											url,
											null,
											true,
											loadOperatorListCallback,
											"get");
				loadOperatorList_ajax.send();
			}
			
			/* -----接收AJAX返回信息_获取操作员列表-----*/
			function loadOperatorListCallback(){
				var oTBody = document.getElementById("tbody");
				var oRow;
				var oCellId;
				//alert(oTBody.rows.length)
				while(oTBody.rows.length>0){
					  oTBody.deleteRow();
				}
				var result = arguments[0];
				//alert(result);
				if(result==null || result==""){
					oRow = document.createElement("TR");
					oRow.className= "even1";
					oTBody.appendChild(oRow);
					oCellId = document.createElement("TD");
					oRow.appendChild(oCellId);
					oCellId.setAttribute("colspan","4",0);
					oCellId.setAttribute("align","center");
					oCellId.innerText = "--- 没有找到相关信息 ---";
				}else{
					var myData = JSON.parse(result,null);
					document.getElementById("pageStr").innerHTML=myData.pageStr;
					
					for (var i = 0; i < myData.operators.length; i++){
						oRow = document.createElement("TR");
						if (i % 2 == 0){
							oRow.className= "even1";
						}else{
							oRow.className= "even2";
						}
						oTBody.appendChild(oRow);
						oCellId = document.createElement("TD");
						oCellId.innerText = myData.operators[i].id;
						oRow.appendChild(oCellId);
						
						oCellName = document.createElement("TD");
						oCellName.innerText = myData.operators[i].loginName;
						oRow.appendChild(oCellName);
						
						oCellTmp = document.createElement("TD");
						oCellTmp.innerText = myData.operators[i].addDate;
						oRow.appendChild(oCellTmp);
						
						oCellOp = document.createElement("TD");
						oCellOp.innerHTML = '<input type="button" value="编辑" ref="009" onclick="javascript:showDiv(' + myData.operators[i].id + ',editOPCallBack);"/>&nbsp;&nbsp;&nbsp;<input type="button" ref="010" value="删除" onclick="javascript:deleteOperator(' + myData.operators[i].id + ',\'' + myData.operators[i].loginName + '\');"/>';
						oRow.appendChild(oCellOp);
					}
				}
				initDiv(user_moduleList);
			}	
			/*---- add ajax and edit ajax-----*/
			function submitAdd(){
				var form = document.forms['OperatorForm'];
				authorizeInit(form);
				var submitType = new Array();
				
				//校验form是否合法
				if  (!checkGroupForm(form)){
					return;
				}
				
				/*--- 判断操作类型 进行form action的修改 ---*/
				if (form.btnAction.innerText == '修改'){
					form.action = "/admin/editOperatorAjax.do";
					submitType.push("modify");
				}else{
					submitType.push("add");
				}
				var addGroup_ajax = new Ajax(form.action,formToRequestString(form),true,addOPCallback,"post",submitType);
				addGroup_ajax.send();
			}
			

			function addOPCallback(){
				var result = arguments[0];
				var msgs = result.split("||");
				
				alert(msgs[1]);
								
				var type = arguments[1];
				if(msgs[0] == "00"){
					loadOperatorList("/admin/loadOperatorListAjax.do?pageSize=" + document.getElementById("pageSize").value);
				}
				closeDiv();
			}
			
			function checkGroupForm(form){
				alert(form.authorize.value);
				alert(form.parentId.value);
				return true;
			}

			/* -----发送AJAX请求_删除操作员----*/
			function deleteOperator(oid,oName){
				if(confirm('确定要删除操作员吗？[id:' + oid + '   ' + oName + ']')==false){return false;}
				var deleteOperator_ajax = new Ajax(
									"/admin/deleteOperatorAjax.do?oid=" + oid,
									null,
									true,
									deleteOperatorCallback,
									"get");
				deleteOperator_ajax.send();
			}
			
			/* -----接收AJAX返回信息_删除操作员-----*/
			function deleteOperatorCallback(){
				var result = arguments[0];
				var values = result.split("||");
				alert(values[1]);
				if(values[0] == "00"){
					loadOperatorList("/admin/loadOperatorListAjax.do?pageSize=" + document.getElementById("pageSize").value);
				}
			}

		</script>
	</head>

	<body>

	<button ref="008" onClick="showDiv(null,addOPCallBack);" id="btnTest">添加操作员</button>

	<div>
		<TABLE width="95%" align="center" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF" style="border:1px solid #56626d;font-size:12px">
			<THEAD>
			  <TR bgcolor="#FFFFFF">
			    <TD colspan="3" align="left" style="font-size:14px"><B>&nbsp;&nbsp;操作员列表</B></TD>
			  </TR>
			  <TR bgcolor="#FFFFFF">
			    <TH width="5%" style="background:#46525d;text-align: center;">
			    	<span style="background:#46525d;color:#FFFFFF;text-align: center;">
			    		编号
					</span>
				</TH>
			    <TH width="15%" style="background:#46525d;text-align: center;">
			    	<span style="background:#46525d;color:#FFFFFF;text-align: center;">
			    		名称
					</span>
				</TH>	
			    <TH width="10%" style="background:#46525d;text-align: center;">
			    	<span style="color:#FFFFFF">
			    		建立日期
			    	</span>
			    </TH>
			    <TH width="10%" style="background:#46525d;text-align: center;">
			    	<span style="color:#FFFFFF">
			    		操作
			    	</span>
			    </TH>
			  </TR>
			</THEAD>
			
			<TBODY id="tbody">

			</TBODY>
		</TABLE>
	</div>
		<div class="spanDiv"></div>
		<div id="pageStr"></div>

		<div class="test" id="moveDiv">
			<html:form method="POST" action="/admin/addOperatorAjax" >
				<table width="360" height="400">

				<tr style="border:1px solid #56626d;">
				<td align="right"><label for="loginName">用户名：</label></td><td><input type="text" name="loginName"></td>
				</tr>
				<tr>
					<td align="right"><label for="password">密&nbsp;&nbsp;码：</label></td>
					<td><input type="password" name="password"></td>
				</tr>
				<tr>
					<td align="right"><label for="confrmPwd">确认密码：</label></td><td><input type="password" name="confrmPwd"></td>
				</tr>
				<tr>
					<td align="right"><label for="hasChild">是否允许增加子操作员：</label></td><td><input type="checkbox" name="hasChild"></td>
				</tr>
				<tr>
					<td align="right"><label for="introduction">介绍：</label></td><td><textarea name="introduction"></textarea></td>
				</tr>
				<tr>
					<td align="right">权限组选择：</td><td></td>
				</tr>
				<tr>
					<td align="center" colspan="2">
						<table>
							<tr>
								<td>
								<select size="10"  multiple name="from" class="selectStyle" id="from"  >
								</select>
								</td>
								<td>
									<button onClick="copyAll('from','to');">->></button><br/>
									<button onClick="copyToList('from','to');">--></button><br/>
									<button onClick="copyToList('to','from');"><--</button><br/>
									<button onClick="copyAll('to','from');"><<-</button>
								</td>
								<td>
									<select size="10"  multiple name="to" class="selectStyle">
									</select>
									<input type="hidden" name="parentId" value=""/>
									<input type="hidden" name="authorize" value=""/>
									<input type="hidden" name="id" value=""/>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td align="center" colspan="2"><button onClick="submitAdd(this);" name="btnAction">添加</button>&nbsp;&nbsp;&nbsp;<button onClick="showDiv(null,addOPCallBack);" name="btnCancel">取消</button>&nbsp;&nbsp;&nbsp;<button onClick="closeDiv();">关闭</button></td>
				</tr>
				</table>
			</html:form>
		
		</div>
			<script language="javascript" type="text/javascript">
		  		loadOperatorList("/admin/loadOperatorListAjax.do");
		  	</script>
	</body>
</html:html>
